/*
 * @lc app=leetcode.cn id=203 lang=rust
 *
 * [203] 移除链表元素
 */

// @lc code=start
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
        let mut root = ListNode{val: val, next: head};
        let mut head = &mut root;
        while let Some(ref mut node) = head.next {
            if node.val == val {
                head.next = node.next.take();
            }
            else{
                head = head.next.as_mut()?;
            }
        }
        
        return root.next;
    }
}
// @lc code=end

